<?php

namespace addons\zzyy\library;


use fast\Date;

class Ykxbon
{
    protected $_timestamp = null;
    protected $_displaytimestamp = null;

    static public function now()
    {
        $ins = new self();
        $ins->_timestamp = time();
        return $ins;
    }

    static public function parse($str)
    {
        $ins = new self();
        $ins->_timestamp = strtotime($str);
        return $ins;
    }

    static public function create($year, $month, $day, $hour, $min)
    {
        $ins = new self();
        $ins->_timestamp = mktime($hour, $min, 0, $month, $day, $year);
        return $ins;
    }

    static public function createFromTimestamp($timestamp)
    {
        $ins = new self();
        $ins->_timestamp = $timestamp;
        return $ins;
    }

    public function format($format = 'Y-m-d H:i:s')
    {
        return date($format, $this->_timestamp);
    }

    public function getTimestamp()
    {
        return $this->_timestamp;
    }

    public function getTimestampMs()
    {
        return $this->_timestamp . '001';
    }

    public function getYear()
    {
        return date('Y', $this->_timestamp);
    }

    public function getMonth()
    {
        return date('m', $this->_timestamp);
    }

    public function getDay()
    {
        return date('d', $this->_timestamp);
    }

    public function getHour()
    {
        return date('H', $this->_timestamp);
    }

    public function getMin()
    {
        return date('m', $this->_timestamp);
    }

    public function getSec()
    {
        return date('s', $this->_timestamp);
    }

    public function startOfDay()
    {
        $t = Date::unixtime('day', 0, 'begin', date('Y', $this->_timestamp), date('m', $this->_timestamp), date('d', $this->_timestamp));
        $this->_displaytimestamp = $t;

        return $this;
    }

    public function endOfDay()
    {
        $t = Date::unixtime('day', 0, 'end', date('Y', $this->_timestamp), date('m', $this->_timestamp), date('d', $this->_timestamp));
        $this->_displaytimestamp = $t;

        return $this;
    }

    public function toDateTimeString()
    {
        if (!is_null($this->_displaytimestamp)) {
            $kk = date('Y-m-d H:i:s', $this->_displaytimestamp);
            $this->_displaytimestamp = null;
            return $kk;
        }
        return date('Y-m-d H:i:s', $this->_timestamp);
    }

    public function toDateString()
    {
        if (!is_null($this->_displaytimestamp)) {
            $kk = date('Y-m-d', $this->_displaytimestamp);
            $this->_displaytimestamp = null;
            return $kk;
        }
        return date('Y-m-d', $this->_timestamp);
    }

    public function betweenIncluded($start, $end)
    {
        if ($start instanceof Ykxbon) {
            $startTs = $start->getTimestamp();
        } else if (is_numeric($start)) {
            $startTs = $start;
        } else if (is_string($start)) {
            $startTs = strtotime($start);
        }

        if ($end instanceof Ykxbon) {
            $endTs = $end->getTimestamp();
        } else if (is_numeric($end)) {
            $endTs = $end;
        } else if (is_string($end)) {
            $endTs = strtotime($end);
        }

        if ($this->_timestamp >= $startTs and $this->_timestamp <= $endTs) {
            return true;
        } else {
            return false;
        }
    }

    public function between($start, $end)
    {
        if ($start instanceof Ykxbon) {
            $startTs = $start->getTimestamp();
        } else if (is_numeric($start)) {
            $startTs = $start;
        } else if (is_string($start)) {
            $startTs = strtotime($start);
        }

        if ($end instanceof Ykxbon) {
            $endTs = $end->getTimestamp();
        } else if (is_numeric($end)) {
            $endTs = $end;
        } else if (is_string($end)) {
            $endTs = strtotime($end);
        }

        if ($this->_timestamp > $startTs and $this->_timestamp < $endTs) {
            return true;
        } else {
            return false;
        }
    }

    public function isBefore(Ykxbon $t)
    {
        if ($this->_timestamp < $t->getTimestamp()) {
            return true;
        } else {
            return false;
        }
    }

    public function isAfter(Ykxbon $t)
    {
        if ($this->_timestamp > $t->getTimestamp()) {
            return true;
        } else {
            return false;
        }
    }

    public function addMinute($m = 1)
    {
        $this->_timestamp += 60 * $m;
        return $this;
    }

    public function subMinute($m = 1)
    {
        $this->_timestamp -= 60 * $m;
        return $this;
    }

    public function addHour($h = 1)
    {
        $this->_timestamp += 3600 * $h;
        return $this;
    }

    public function subHour($h = 1)
    {
        $this->_timestamp -= 3600 * $h;
        return $this;
    }

}